   
# from tensorflow.keras.models import Model, load_model


# print("加载模型......")
# model_save_path = "/opt/wyh/LSP_book_rec/DSSM/Saved_models/TestUniversity"
# print("model_save_path",model_save_path)
# model = load_model(model_save_path) 
# print("加载完毕......")


# import re

# # 假设这是你的文本数据
# text = """
# 书单名：《知行岁月，美与逻辑》

# 书单简介：
# 汇集历史、经济、逻辑与美学，本书单跨越时空，从中国建设五十年到世界大美，从陈赓的日记到鲁迅的书信，每本书都是知识的瑰宝，带领读者在知行岁月中探寻美与逻辑的交织。"""

# # 使用正则表达式来匹配和提取所需信息，这里使用了更宽松的匹配方式
# # 尝试匹配“书单名”和紧随其后的文本，以及可能的简介部分
# pattern = r"书单名：(.*?)\n*(?:\n+|\Z)(.*)"
# match = re.search(pattern, text, re.DOTALL)

# # 检查是否匹配成功，并提取结果
# if match:
#     book_list_name = match.group(1).strip()
#     # 尝试提取简介部分，如果存在的话
#     book_list_description = match.group(2).strip() if match.group(2).strip() else "未找到书单简介"
# else:
#     book_list_name = "未找到书单名"
#     book_list_description = "未找到书单简介"

# # 输出提取的结果
# print("书单名:", book_list_name)
# print("书单简介:", book_list_description)


# import re

# # 假设这是你的文本数据
# text = """
# 书单名：万象博览·知行合一

# 书单简介：
# 汇集古今中外，从侠义传奇到学术探究，从艺术哲学到法律实务，本书单带你领略知识的广阔天地。万象博览，知行合一，每本书都是一段别致的旅程，一场思想的盛宴。
# """

# # 使用正则表达式来匹配和提取所需信息，这里使用了更宽松的匹配方式
# # 允许在关键词和内容之间有任意数量的空格或换行
# pattern = r"书单名：\s*(.*?)\s*\n\n书单简介：\s*(.*?)\s*(?:\n|$)"
# match = re.search(pattern, text, re.DOTALL)

# # 检查是否匹配成功，并提取结果
# if match:
#     book_list_name = match.group(1).strip()
#     book_list_description = match.group(2).strip()
# else:
#     book_list_name = "未找到书单名"
#     book_list_description = "未找到书单简介"

# # 输出提取的结果
# print("书单名:", book_list_name)
# print("书单简介:", book_list_description)


# 测试从milvus查询数据


# from pymilvus import connections, Collection, FieldSchema, CollectionSchema, DataType, utility, Index


# def write_to_milvus(collection_name, entities):
#     connections.connect("default", host="10.240.1.3", port="19530")
#     if not utility.has_collection(collection_name):
#         collection = create_collection(collection_name)
    
#     collection = Collection(name=collection_name)
#     # 插入数据
#     insert_result = collection.insert(entities)
#     collection.flush()

#     # 创建索引
#     index = {
#         "index_type": "IVF_FLAT",
#         "metric_type": "L2",
#         "params": {"nlist": 128},
#     }
#     collection.create_index(field_name="embedding", index_params=index)

#     # print("写入Milvus成功......")

# def query_milvus(query_vectors,collection_name='CUPL', top_k=10):
#     # 连接到Milvus服务
#     connections.connect("default", host="10.240.1.3", port="19530")

#     # 获取集合对象
#     collection = Collection(name=collection_name)

#     # 定义搜索参数
#     search_param = {
#         "metric_type": "L2",  # 使用L2距离作为相似度度量
#         "params": {"nprobe": 10},  # nprobe是IVF索引搜索时的参数，影响搜索精度和速度
#     }

#     # 执行搜索
#     results = collection.search(
#         data=query_vectors,  # 查询向量
#         anns_field="embedding",  # 向量字段名
#         param=search_param,  # 搜索参数
#         limit=top_k,  # 返回的最相似的top k个结果
#         expr=None  # 过滤表达式，如果需要可以设置
#     )

#     # 处理搜索结果
#     for result in results:
#         # print(result)
#         print(transfer(eval(str(result))))

# import re
# def transfer(elements):
#     ids = []
#     # print(elements)
#     for element in elements:
#         match = re.search(r'id: (\d+)', element)
#         if match:
#             ids.append(int(match.group(1)))  # 将提取的id转换为整数类型
#     return ids
# # query_vectors = [[0.009632518727744621,0.5334368870468407,0.8679393924705094,0.9579711721810709,0.9648111900043539,0.1399386078469531,0.912633685651479,0.8407262700073588,0.7479956754963284,0.7285998848979216,0.744525758258753,0.20683267010877104,0.4342161292503155,0.8798273480029277,0.331977111710559,0.7480174353084275,0.181652280041531,0.609926826252742,0.6541695696416296,0.7439945364129892,0.9022301435348115,0.7947736475480671,0.9421626165263004,0.848775015704776,0.29770212076050995,0.21411450378783226,0.3411634181106866,0.23444549623473732,0.062281774654177946,0.7540838657852647,0.03435764005166608,0.6486959978076237,0.5477688474959046,0.1945693622787814,0.3090883432122338,0.009726105524253237,0.8630482636756125,0.14719295275394972,0.7414201817357406,0.66007556794127,0.5917238603801518,0.21113622633237528,0.8195694017798392,0.6997592844442517,0.0317230722631745,0.3969830119105804,0.3878121445744944,0.40154926487031184,0.451547796876006,0.1378971590283764,0.7864487885981775,0.16097340135541938,0.5734277305552979,0.7781718358450878,0.3867744463384548,0.22202665958876122,0.29034615378093465,0.9299774643926924,0.9511086537227096,0.8283482535743225,0.8988746113205706,0.8107868421896345,0.5522036127772465,0.3367793657335185,0.40630527744708633,0.24053037336814698,0.76411373449837,0.29474028455289725,0.987284714512674,0.2961444836599698,0.019190752659339783,0.34539915217740114,0.2555852075667895,0.3445805614837667,0.06306100333436904,0.9676152168088938,0.37448534011369294,0.32466220644482635,0.94467913968618,0.1036448144647304,0.9908578502775287,0.5185552108465585,0.726620049876473,0.28261309424583825,0.5452726454460946,0.7130081007644207,0.025507358643088063,0.8874560981932846,0.8986878412105042,0.5638889770402722,0.10129445029675521,0.1965169589889395,0.23668171056437148,0.5962231859954348,0.05883880478612302,0.030194111184048467,0.8947362814354418,0.31851466896113556,0.8117170908001874,0.6803885923784554,0.6925368088621291,0.03429759648918518,0.1692410199294756,0.0719654792785509,0.3542877899266379,0.9598104643678882,0.6135721439932125,0.3626446968179309,0.6464041940626415,0.4525148321200254,0.07704910873811666,0.5910021025531993,0.19718417084458872,0.8537537454367972,0.7323410900134169,0.1379442525309147,0.8017473617036939,0.30422027574620647,0.05576080763448865,0.7570504557390374,0.4748952044656427,0.973173327673253,0.05586315512136797,0.6833011328713363,0.32405423481527196,0.2666331196006284,0.6564051801051862,0.4106333876680497]]
# # query_milvus(query_vectors,'CUPL')

# query_vectors = [[0,0,0,0,0,0,0.0749043,0,0,0,0,0.14286609,0,0.1048718,0,0,0,0,0.1364050,0.1163446,0,0,0.1197541,0.11759895,0.1494680,0,0,0.0731338,0.1460707,0,0.1482453,0,0,0,0.1469501,0.17299968,0,0,0.1184003,0.1220089,0.0849424,0,0,0.1328910,0,0.1364602,0.1549234,0,0,0,0.1582304,0,0.1140577,0,0,0,0,0.1269418,0,0.20440698,0.1470685,0,0.0907621,0,0.1426276,0,0,0,0,0.1399421,0,0.16249126,0.0632804,0,0.1307214,0.1502426,0.1766859,0.13130246,0.1158375,0.1651457,0,0.1074586,0,0,0.1372076,0.1137434,0.1549760,0,0.1196332,0,0,0,0,0.0822020,0.1393556,0,0.1167439,0.1499223,0,0,0,0,0.0717611,0.0273339,0.1568374,0,0.1582386,0.08864147,0.0911933,0,0.1667152,0.1360321,0,0,0,0,0,0,0,0.11099213,0.1128167,0.1180809,0.1527778,0,0,0.16303325,0,0.08912656]]
# query_milvus(query_vectors,'CUPL')

# import faiss 
# import numpy as np
# item_path = "/opt/wyh/LSP_book_rec/Upload/Dataset/YBU/embedding/item_embedding.index"
# user_path = "/opt/wyh/LSP_book_rec/Upload/Dataset/YBU/embedding/user_embedding.npy"
# item_index = faiss.read_index(item_path)
# user_embeddings = np.load(user_path)

# index = 3411
# user_embedding = user_embeddings[index].reshape(1,-1)
# print(user_embedding.shape)

# print(type(user_embedding))
# item_embedding,I = item_index.search(np.array(query_vectors),50)
# item_ids = I[0].tolist()
# print(item_ids)

# print(item_embedding)












































